<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<%
    String host = request.getHeader("Host");
    String ip = host.substring(0, host.length() - 5);
    HashMap<String, String> serverIpsMap = (HashMap<String, String>) request.getServletContext().getAttribute("serverIpsMap");
    String env = serverIpsMap.get(ip);
    if (env == null) {
        request.setAttribute("env", ip);
    } else {
        request.setAttribute("env", env);
    }
%>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8"/>
    <title>Index of ${env}</title>
    <meta name="description" content="index"/>
</head>
<body>
<h1>Index</h1>
<label for="servers">Go to: </label>
<select id="servers" name="servers">
    <option value="${serverIpsMap["DEV"]}">DEV</option>
    <option value="${serverIpsMap["ST"]}">ST</option>
    <option value="${serverIpsMap["SIT"]}">SIT</option>
    <option value="${serverIpsMap["LOCAL"]}">LOCAL</option>
</select>
<button type="button" onclick="goToServer()">Go</button>
<ol>
    <li>
        <h3><a href="${ctx}/destination?m=send" target="_blank">Send Message</a></h3>
    </li>
    <li>
        <h3><a href="${ctx}/destination?m=receive" target="_blank">Receive Message</a></h3>
    </li>
    <li>
        <h3><a href="${ctx}/destination?m=count" target="_blank">Count Message</a></h3>
    </li>
    <li>
        <h3><a href="${ctx}/dispatch?m=rest_test" target="_blank">REST Tester</a></h3>
    </li>
    <li>
        <h3><a target="_blank" onclick="token()">Token Result: POST /opentext/oauth</a></h3>
    </li>
    <li>
        <h3><a target="_blank">Download Result: POST /opentext/download</a></h3>
    </li>
    <li>
        <h3><a href="${ctx}/config/reload" target="_parent">Reload config data for queue details</a></h3>
    </li>
    <li>
        <h3><a href="${ctx}/task/taskList" target="_blank">Task Management</a></h3>
    </li>
</ol>
<div style="position:absolute;right:10px;top:10px;" id="result">Version: 20160507</div>
<span style="clear:both; z-index:1000; overflow:hidden; bottom:10px; left:5px;position:fixed !important;color: #e9e9e9">Author: Charkey@ODC;Cooperator: 大头森@ODC</span>
<script type="application/javascript" src="${ctx}/static/jquery-2.0.3.min.js"></script>
<script type="application/javascript">
    $(function(){
        $("#servers").find("option[value='${serverIpsMap[env]}']").attr("selected", "selected");
    });

    function receiveMessage() {
        alert("还没实现，懒的写啦");
        e.preventDefault();
    }
    function token() {
        var path = window.location.pathname.split("/")[0];
        $.ajax({
            type: "POST",
            url: path + "/opentext/oauth",
            data: {},
            dataType: 'json',
            beforeSend: function () {
            },
            success: function (data) {
                console.info(JSON.stringify(data));
                alert(JSON.stringify(data));
            },
            error: function (XMLHttpRequest, errorThrown) {

            }
        });
    }
    function downloadImage() {
        var path = window.location.pathname.split("/")[0];
        $.ajax({
            type: "POST",
            url: path + "/opentext/download",
            data: {},
            dataType: 'json',
            beforeSend: function () {
            },
            success: function (data) {
                console.info(JSON.stringify(data));
                alert(JSON.stringify(data));
            },
            error: function (XMLHttpRequest, errorThrown) {

            }
        });
    }

    function goToServer() {
        window.location.href = "http://" + $("#servers").find("option:selected").val() + ":8789";
    }
</script>
</body>
</html>

